package com.ylxl.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ylxl.common.annotation.Log;
import com.ylxl.common.enums.BusinessType;
import com.ylxl.common.result.AjaxResult;
import com.ylxl.common.result.BaseController;
import com.ylxl.common.utils.StringUtils;
import com.ylxl.entity.Label;
import com.ylxl.entity.reponse.ContainerVO;
import com.ylxl.entity.request.ContainerCmdOrMdy;
import com.ylxl.entity.request.QueryContainer;
import com.ylxl.service.IContainerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 集装箱管理
 * @Auther Gilbert
 * @Date 2023/6/26 13:47
 */
@Slf4j
@RestController
@RequestMapping("/container")
@Api(tags = "集装箱管理", hidden = true)
public class ContainerController extends BaseController {

    @Resource
    private IContainerService containerService;
    /**
     * 分页查询集装箱列表信息
     * @param queryContainer
     * @return
     */
    @Log(title = "分页查询集装箱列表信息", businessType = BusinessType.SELECT)
    @PostMapping("/queryContainerList")
    @ApiOperation(value = "分页查询集装箱列表信息", response = AjaxResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ContainerVO.class),})
    public AjaxResult queryContainerList(@RequestBody QueryContainer queryContainer) {
        AjaxResult ajax = AjaxResult.success();
        try {
            //进行分页操作
            PageInfo<ContainerVO> pageInfo = new PageInfo<>();
            if (StringUtils.isNotEmpty(queryContainer.getPageNum()) && StringUtils.isNotEmpty(queryContainer.getPageSize())) {
                PageHelper.startPage(queryContainer.getPageNum(), queryContainer.getPageSize());
            }
            List<ContainerVO> queryContainers = containerService.selectContainerList(queryContainer);
            pageInfo = new PageInfo<>(queryContainers);
            ajax.put(AjaxResult.DATA_TAG, queryContainers);
            ajax.put(AjaxResult.TOTAL, pageInfo.getTotal());
        } catch (Exception e) {
            log.error("查询集装箱列表日志信息，异常信息：{}", e);
            return toAjax(false);
        }
        return ajax;
    }
    /**
     * 集装箱保存
     * @param containerCmdOrMdy
     * @return
     */
    @Log(title = "集装箱保存", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    @ApiOperation(value = "集装箱保存", response = AjaxResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Integer.class),})
    public AjaxResult add(@RequestBody ContainerCmdOrMdy containerCmdOrMdy){

        return toAjax(containerService.insertContainer(containerCmdOrMdy));
    }

    /**
     * 集装箱编辑
     * @param containerCmdOrMdy
     * @return
     */
    @Log(title = "集装箱编辑", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation(value = "集装箱编辑", response = AjaxResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Integer.class),})
    public AjaxResult update(@RequestBody ContainerCmdOrMdy containerCmdOrMdy){
        return toAjax(containerService.updateContainer(containerCmdOrMdy));
    }
    /**
     * 集装箱根据id查询明细
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "集装箱根据id查询明细", response = AjaxResult.class)
    @Log(title = "集装箱根据id查询明细", businessType = BusinessType.SELECT)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Label.class),})
    public AjaxResult getInfo(@PathVariable(value = "id", required = true) Integer id) {
        return AjaxResult.success(containerService.selctById(id));
    }
    /**
     * 集装箱根据id删除
     */
    @PostMapping("/remove/{id}")
    @ApiOperation(value = "集装箱根据id删除", response = AjaxResult.class)
    @Log(title = "集装箱根据id删除", businessType = BusinessType.DELETE)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Integer.class),})
    public AjaxResult remove(@PathVariable(value = "id", required = true) Integer id) {
        return AjaxResult.success(containerService.deleteById(id));
    }
}
